<?php

declare(strict_types=1);

namespace App\Service\CustomService;

use App\Model\CommodityModel\Custom;
use App\Service\BaseService;
use App\Service\Code\Normal;
use Hyperf\DbConnection\Db;

class CustomService extends BaseService
{
    const CACHE_CUSTOM_KEY = 'CACHE_CUSTOM_KEY';

    /**
     * 查询关区列表
     * @param array $param
     * @return array
     */
    public function queryCustomList(array $param = [])
    {
        $redis = redis();
        $cache = $redis->get(self::CACHE_CUSTOM_KEY);
        if (empty($cache)) {
            $customs = Custom::instance()->where([
                'IsDeleted' => 0
            ])->select([
                Db::raw('CONCAT(ID,"") AS custom_id'),
                Db::raw('Name as custom_name'),
                Db::raw('Code as custom_code')
            ])->get()->toArray();
            $redis->set(self::CACHE_CUSTOM_KEY, json_encode($customs));
        }else{
            $customs = json_decode($cache, true);
        }
        return Normal::returnCode(Normal::SUC, "获取成功", $customs);
    }


    /**
     * 查询关区信息
     * @param array $param
     * @return array
     */
    public function queryCustomInfo(array $param = [])
    {
        $customId   = $param['customId'] ?? 0;
        $customs    = $this->queryCustomList();
        $customMap  = array_column($customs['data'],NULL,'custom_id');
        $customInfo = $customMap[$customId] ?? [];
        return Normal::returnCode(Normal::SUC, "获取成功", $customInfo);
    }

}
